﻿using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Terraria;
using TerrariaApi.Server;
using TShockAPI;

namespace TShock_Gift
{
    [ApiVersion(2, 1)]
    public class TShock_Gift : TerrariaPlugin
    {
        public override string Author => "星夜神花-QQ群:1103642210";
        public override string Description => "在线礼物插件";
        public override string Name => "TShock_Gift";
        public override Version Version => new Version(0, 0, 0, 3);
        public TShock_Gift(Main game) : base(game)
        {
        }

        Timer timer;
        Config config;
        public override void Initialize()
        {
            config = Config.LoadConfig();
            Commands.ChatCommands.Add(new Command(GetProbability, "probability"));

            TShockAPI.Hooks.GeneralHooks.ReloadEvent += GeneralHooks_ReloadEvent;
            timer = new Timer(Timer_Elapsed,"", config.TimeUnit, config.TimeUnit);
        }

        private void GeneralHooks_ReloadEvent(TShockAPI.Hooks.ReloadEventArgs e)
        {
            config = Config.LoadConfig();
            timer.Change(config.TimeUnit, config.TimeUnit);
        }

        Dictionary<string, int> players = new Dictionary<string, int>();
        public static int Day = DateTime.Now.Day;
        private void Timer_Elapsed(object state)
        {
            if (config.Enable == false)
            {
                return;
            }
            if (DateTime.Now.Day != Day)
            {
                Day = DateTime.Now.Day;
                players.Clear();
            }
            for (int i = 0; i < TShock.Players.Length; i++)
            {
                TSPlayer player = TShock.Players[i];
                if (player == null)
                {
                    continue;
                }
                if (player.Active && player.IsLoggedIn)
                {
                    if (player.PlayerData.maxHealth >= 400)
                    {
                        continue;
                    }
                    if (players.ContainsKey(player.Name) == false)
                    {
                        players[player.Name] = 0;
                    }
                    else
                    {
                        players[player.Name] += 1;
                    }
                    if (config.PointOfTime.ContainsKey(players[player.Name]))
                    {
                        Gift gift = RandGift();
                        if (gift == null)
                        {
                            return;
                        }
                        int number = new Random().Next(gift.number[0], gift.number[1]);
                        player.GiveItem(gift.id, number);
                        player.SendMessage(string.Format(config.PointOfTime[players[player.Name]] + "获得在线奖励[i/s{2}:{1}]", players[player.Name], gift.id, number), Color.GreenYellow);
                    }
                    else if (players[player.Name] % 10 == 0)
                    {
                        player.SendMessage(string.Format(config.DefaultMessage, players[player.Name]), Color.GreenYellow);
                    }
                }
            }
        }


        private void GetProbability(CommandArgs args)
        {
            Task.Run(() => {
                for (int i = 0; i < config.Gifts.Count; i += 10)
                {
                    StringBuilder sb = new StringBuilder();
                    for (int j = 0; j < 10 && i + j < config.Gifts.Count; j++)
                    {
                        Gift gift = config.Gifts[i + j];
                        sb.Append("[i/s1:{0}]:{1:0.##}% ".SFormat(gift.id, 100.0 * ((double)gift.proportion / config.denominator)));
                    }
                    args.Player.SendMessage(sb.ToString(), Color.Cornsilk);
                }
            });
        }

        Random rand = new Random();
        public Gift RandGift()
        {
            int index = rand.Next(config.denominator);
            int sum = 0;
            for (int i = 0; i < config.Gifts.Count; i++)
            {
                sum += config.Gifts[i].proportion;
                if (index < sum)
                {
                    return config.Gifts[i];
                }
            }
            return null;
        }
    }
}
